﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.ComponentModel;
using System.Windows.Forms;

namespace hpm
{
    class clsKhoa
    {
        string _maKhoa;
        string _tenKhoa;

        DAL da = new DAL();
        public DataTable dtbKhoa;
        SqlCommand slqcom;
        SqlDataAdapter sqlDataAdapter = null;

        public clsKhoa()
        {
            this._maKhoa = "";
            this._tenKhoa = "";
        }

        public clsKhoa(string maKhoa, string tenKhoa)
        {
            this._maKhoa = maKhoa;
            this._tenKhoa = tenKhoa;
        }

        public string MaKhoa
        {
            get { return _maKhoa; }
            set { _maKhoa = value; }
        }

        public string TenKhoa
        {
            get { return _tenKhoa; }
            set { _tenKhoa = value; }
        }

        public DataTable GetAll_Khoa()
        {
            da.ketnoiServer();
            dtbKhoa = new DataTable();
            this.slqcom = new SqlCommand();
            this.slqcom.CommandText = "Select * from DMKhoa";
            this.slqcom.Connection = da.con;
            this.slqcom.CommandType = CommandType.Text;
            this.sqlDataAdapter = new SqlDataAdapter(this.slqcom);
            this.sqlDataAdapter.Fill(dtbKhoa);
            //da.da_closeConnect();
            return dtbKhoa;
        }

        public void CapNhatKhoa()
        {
            try
            {
                SqlCommandBuilder cb = new SqlCommandBuilder(sqlDataAdapter);
                sqlDataAdapter.Update(dtbKhoa);
            }
            catch
            {
                MessageBox.Show("Không thể thêm dữ liệu");
            }
        }


        public DataTable ConvertToDataTable<T>(IList<T> data)
        {
            PropertyDescriptorCollection properties =
               TypeDescriptor.GetProperties(typeof(T));
            DataTable table = new DataTable();
            foreach (PropertyDescriptor prop in properties)
                table.Columns.Add(prop.Name, Nullable.GetUnderlyingType(prop.PropertyType) ?? prop.PropertyType);
            foreach (T item in data)
            {
                DataRow row = table.NewRow();
                foreach (PropertyDescriptor prop in properties)
                    row[prop.Name] = prop.GetValue(item) ?? DBNull.Value;
                table.Rows.Add(row);
            }
            return table;
        }
    }
}
